package com.liyunc.demo.config;
/**
 * Copyright (c) 2018-2022 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.cors.CorsConfiguration;
import com.liyunc.demo.service.UserService;

import java.util.List;

/**
 * @author liyuncong
 * @version 1.0
 * @file
 * @brief
 * @details config
 * @date 2022-02-17
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2022-02-17               liyuncong        Created
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
    securedEnabled = true,
    prePostEnabled = true,
    jsr250Enabled = true
)
public class AppSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        List<String> allowedOrigins = List.of("http*://localhost**", "http*://127.0.0.**",
            "http*://10.4.12.**");
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedHeaders(List.of("Content-Type", "AuthToken"));
        configuration.setAllowedOriginPatterns(allowedOrigins);
        configuration.setAllowCredentials(Boolean.FALSE);
        configuration.setAllowedMethods(List.of("GET", "POST", "DELETE"));

        http.authorizeRequests()
            .antMatchers("/api/v1/auth/**", "/api/v1/config/**")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .formLogin()
            .usernameParameter("username").passwordParameter("password")
            .loginPage("/ui/login")
            .permitAll()
            .loginProcessingUrl("/api/v1/auth/form")
            .permitAll()
            .defaultSuccessUrl("/ui/home")
            .and()
            .logout()
            .logoutUrl("/ui/logout")
            .logoutSuccessUrl("/ui/login")
            .permitAll()
            .and()
            .csrf()
            .disable()
            .cors()
            .configurationSource(request -> configuration);
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // user Details Service验证
        auth
            .userDetailsService(userDetailsService())
            .passwordEncoder(passwordEncoder());
    }

    /**
     * 注册UserDetailsService 的bean.
     */
    @Bean
    public UserDetailsService userDetailsService() {
        return new UserService();
    }

    /**
     * 指定加密方式.
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        // 使用BCrypt加密密码
        return new BCryptPasswordEncoder();
    }

}
